package com.weishuang.monitor.leader.service;

import com.weishuang.monitor.leader.entity.SysMenu;
import com.weishuang.monitor.leader.entity.SysUser;
import com.weishuang.monitor.leader.entity.vo.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * CacheConfig注解 当前缓存配置
 *
 * @Author 62548
 * @Date 2018/8/1217:42
 */
@Slf4j
@CacheConfig(cacheNames = "sysCacheServer")
@Service
public class SysCacheService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 根据登录名称查询用户信息
     * @param username 用户登录账号
     * @return
     */
    @Cacheable(cacheNames = "sysUserCache",key = "\"getUser_\"+#username",unless = "#result == null")
    public SysUser getUser(String username){
        return userService.getUserByUsername(username);
    }

    /**
     * 根据用户ID查询用户信息
     * @param id    用户ID
     * @return
     */
    @Cacheable(cacheNames = "sysUserCache",key = "\"getUserById_\"+#id")
    public SysUser getUserById(String id){
        return userService.getById(id);
    }

    /**
     * 根据用户查询用户的角色权限
     * @param userId
     * @return
     */
    @Cacheable(cacheNames = "sysMenuCache",key = "\"getSysMenuPermissionByUserCode_\"+#userId")
    public List<SysMenu> getSysMenuPermissionByUserId(String userId){
        return menuService.getSysMenuPermissionByUserId(userId);
    }

    /**
     * 根据用户查询用户的菜单
     * @param userId
     * @return
     */
    @Cacheable(cacheNames = "sysMenuCache",key = "\"getUserMenuListByUserCode_\"+#userId")
    public List<MenuVO> getUserMenuListByUserCode(String userId) {
        return menuService.getUserMenuListByUserId(userId);
    }

    /**
     * 缓存系统字典数据
     * @return
     */
    @Cacheable(key = "\"getDict_sys\"")
    public Map<String, Object> getDict() {
        return dictDataService.getDict();
    }

    /**
     * 清除CascaderList缓存
     */
    @CacheEvict(cacheNames = "CascaderList",allEntries = true)
    public void clearOfficeListByOfficeCode(){
        log.info("清除Cascader缓存！");
    }

    /**
     * 清除系统字典数据
     */
    @CacheEvict(key = "\"getDict_sys\"")
    public void clearDict() {
        log.info("清除系统数据字典缓存！");
    }

    /**
     * 退出登录
     * @param username
     */
    @CacheEvict(cacheNames = "sysUserCache",key = "\"getUser_\"+#username")
    public void logout(String username){
        log.info("用户名："+username+"退出登录清除缓存！");
    }

    @Caching(evict = {@CacheEvict(cacheNames = "sysMenuCache",key = "\"getSysMenuPermissionByUserCode_\"+#userId"),
            @CacheEvict(cacheNames = "sysMenuCache",key = "\"getUserMenuListByUserCode_\"+#userId")})
    public void clearSysMenuPermissionByUserCode(String userId) {
        log.info("用户Code:{},清除菜单与权限缓存！",userId);
    }

    /**
     * 清除用户菜单与权限缓存
     */
    @CacheEvict(cacheNames = "sysMenuCache",allEntries = true)
    public void clearSysMenuPermission(){
        log.info("除菜单权限缓存！");
    }

    /**
     * 清除用户登录信息与使用用户的信息
     * TODO 退出session
     * @param userId
     */
    public void clearSysUser(String userId){
        SysUser userById = getUserById(userId);
        logout(userById.getUsername());
        clearSysUserByUserId(userId);
    }

    /**
     * 清除用户信息，userCode
     * @param userId
     */
    @CacheEvict(cacheNames = "sysUserCache",key = "\"getUserById_\"+#userId")
    public void clearSysUserByUserId(String userId) {
        log.info("用户Code："+userId+"清除用户缓存！");
    }

}
